package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.domain.SurveyRevenueEntity;
import com.ly.heart.mapper.SurveyRevenueMapper;
import com.ly.heart.service.IFileInfoService;
import com.ly.heart.service.ISurveyRevenueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 勘测收益测算Service业务层处理
 * @author wutao
 * @date 2024-05-22
 */
@Service
public class SurveyRevenueServiceImpl extends ServiceImpl<SurveyRevenueMapper, SurveyRevenueEntity> implements ISurveyRevenueService {

    @Resource
    private SurveyRevenueMapper erpSurveyRevenueInfoMapper;
    @Resource
    private IFileInfoService iFileInfoService;

    /**
     * 查询勘测收益测算
     * @param id 勘测收益测算ID
     * @return 勘测收益测算
     */
    @Override
    public SurveyRevenueEntity selectErpSurveyRevenueInfoById (String id) {
        return erpSurveyRevenueInfoMapper.selectErpSurveyRevenueInfoById ( id );
    }

    /**
     * 查询勘测收益测算列表
     * @param erpSurveyRevenueInfo 勘测收益测算
     * @return 勘测收益测算
     */
    @Override
    public List<SurveyRevenueEntity> selectErpSurveyRevenueInfoList (SurveyRevenueEntity erpSurveyRevenueInfo) {
        return erpSurveyRevenueInfoMapper.selectErpSurveyRevenueInfoList ( erpSurveyRevenueInfo );
    }

    /**
     * 新增勘测收益测算
     * @param erpSurveyRevenueInfo 勘测收益测算
     * @return 结果
     */
    @Override
    public int insertErpSurveyRevenueInfo (SurveyRevenueEntity erpSurveyRevenueInfo) {
        //1.处理收益测算数据
        String revenueId = SnowflakeIdWorker.getInstance ().nextStringId ();
        erpSurveyRevenueInfo.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        erpSurveyRevenueInfo.setCreateTime ( DateUtils.getNowDate () );
        //2.处理主数据附件
        uploadFile ( erpSurveyRevenueInfo.getFileEntities (), revenueId );
        //3.处理资方附件和自投附件
        uploadFile ( erpSurveyRevenueInfo.getInvestorFileEntities (), revenueId );
        uploadFile ( erpSurveyRevenueInfo.getSelfFileEntities (), revenueId );
        return erpSurveyRevenueInfoMapper.insertErpSurveyRevenueInfo ( erpSurveyRevenueInfo );
    }

    /**
     * 修改勘测收益测算
     * @param erpSurveyRevenueInfo 勘测收益测算
     * @return 结果
     */
    @Override
    public int updateErpSurveyRevenueInfo (SurveyRevenueEntity erpSurveyRevenueInfo) {
        erpSurveyRevenueInfo.setChangeTime ( DateUtils.getNowDate () );
        //2.处理主数据附件
        uploadFile ( erpSurveyRevenueInfo.getFileEntities (), erpSurveyRevenueInfo.getId () );
        //3.处理资方附件和自投附件
        uploadFile ( erpSurveyRevenueInfo.getInvestorFileEntities (), erpSurveyRevenueInfo.getId () );
        uploadFile ( erpSurveyRevenueInfo.getSelfFileEntities (), erpSurveyRevenueInfo.getId () );
        return erpSurveyRevenueInfoMapper.updateErpSurveyRevenueInfo ( erpSurveyRevenueInfo );
    }

    /**
     * 批量删除勘测收益测算
     * @param ids 需要删除的勘测收益测算ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyRevenueInfoByIds (String[] ids) {
        return erpSurveyRevenueInfoMapper.deleteErpSurveyRevenueInfoByIds ( ids );
    }

    /**
     * 删除勘测收益测算信息
     * @param id 勘测收益测算ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyRevenueInfoById (String id) {
        return erpSurveyRevenueInfoMapper.deleteErpSurveyRevenueInfoById ( id );
    }

    /**
     * 更新附件
     * @param fileEntities
     * @param relationId
     */
    private void uploadFile (List<FileEntity> fileEntities, String relationId) {
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            fileEntities.forEach ( fileEntity -> {
                fileEntity.setRelationId ( relationId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean save = iFileInfoService.saveBatch ( fileEntities );
            if ( ! save ) {
                throw new RuntimeException ( "修改勘测收益附件失败" );
            }
        }
    }

}
